// ignore_for_file: must_be_immutable, sized_box_for_whitespace, unrelated_type_equality_checks

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:time_manager/components/my_card.dart';
import 'package:time_manager/global/controller/record_item_controller.dart';
import 'package:time_manager/view/statistics/dict.dart';
import 'package:time_manager/view/statistics/statistics_controller.dart';

import 'pie/base_pie_entity.dart';
import 'pie/chat_pie.dart';

class StatisticsView extends StatelessWidget {
  StatisticsView({Key? key}) : super(key: key);

  // RecordItemController controller = Get.find<RecordItemController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: const Text('统计'),
      ),
      body: Container(
        color: const Color(0xffeeeeee),
        child: SingleChildScrollView(
          child: Column(
            children: [
              StatisticTop(),
              StatisticBottom(),
              GetBuilder<RecordItemController>(
                // id:"242",
                builder: (_) {
                  if (_.navList.isEmpty) {
                    return SizedBox();
                  }

                  List<PieData> numStatistic = [
                    PieData(
                        _.navList[1].name,
                        double.parse(_.navList[1].num.toString()),
                        Colors.green),
                    PieData(_.navList[2].name,
                        double.parse(_.navList[2].num.toString()), Colors.blue),
                  ];
                  List<PieData> durationStatistic = [
                    PieData(
                        _.navList[4].name,
                        double.parse(_.navList[4].num.toString()),
                        Colors.orange),
                    PieData(_.navList[5].name,
                        double.parse(_.navList[5].num.toString()), Colors.pink),
                  ];

                  if (numStatistic[0].getData() == 0.0 ||
                      numStatistic[1].getData() == 0.0) {
                    return SizedBox();
                  }

                  return MyCard(
                      child: Column(
                    children: [
                      const SizedBox(
                        height: 20,
                      ),
                      StatisticPieChart(datas: numStatistic),
                      const SizedBox(
                        height: 20,
                      ),
                      StatisticPieChart(datas: durationStatistic),
                      const SizedBox(
                        height: 20,
                      ),
                    ],
                  ));
                },
              ),
              const SizedBox(
                height: 20,
              )
            ],
          ),
        ),
      ),
    );
  }
}

class StatisticPieChart extends StatelessWidget {
  // List<PieData> datas = [
  //   PieData("Java", 100.0, Colors.red),
  //   PieData("C++", 100.0, Colors.blue),
  // ];
  final List<PieData> datas;
  StatisticPieChart({Key? key, required this.datas}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PieChart(datas);
  }
}

class PieData extends BasePieEntity {
  final String title;
  final double data;
  final Color color;

  PieData(this.title, this.data, this.color);

  @override
  Color getColor() {
    return color;
  }

  @override
  double getData() {
    return data;
  }

  @override
  String getTitle() {
    return title;
  }
}

class StatisticBottom extends StatelessWidget {
  StatisticBottom({Key? key}) : super(key: key);
  RecordItemController controller = Get.find<RecordItemController>();

  @override
  Widget build(BuildContext context) {
    return MyCard(
      child: Container(
        width: Get.width - 40,
        padding: const EdgeInsets.symmetric(vertical: 20),
        child: Obx(() {
          return Wrap(
            runSpacing: 20,
            children: controller.navList
                .map((it) => Container(
                      // color: Colors.yellow,
                      width: (Get.width - 40) / 3,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(it.name),
                          const SizedBox(
                            height: 10,
                          ),
                          Text(it.num.toString())
                        ],
                      ),
                    ))
                .toList(),
          );
        }),
      ),
    );
  }
}

class StatisticTop extends StatelessWidget {
  StatisticTop({Key? key}) : super(key: key);

  RecordItemController controller = Get.find<RecordItemController>();

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).primaryColor,
      padding: EdgeInsets.all(10),
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Obx(() => Text(
                '学习等级：${controller.currantLevel.value.name}',
                style: TextStyle(color: Colors.white),
              )),
          SizedBox(
            height: 10,
          ),
          // ElevatedButton(
          //     onPressed: () {
          //       controller.updateNav();
          //     },
          //     child: Text('修改')),
          Row(
            children: [
              SizedBox(
                height: 5,
                width: Get.width - 80,
                child: Obx(() => LinearProgressIndicator(
                    value:
                        double.parse(controller.currantLevel.value.rate) / 100,
                    //背景颜色
                    backgroundColor: Colors.grey,
                    //进度颜色
                    valueColor:
                        const AlwaysStoppedAnimation<Color>(Colors.red))),
              ),
              const SizedBox(
                width: 8,
              ),
              Obx(() => Text("${controller.currantLevel.value.rate}%",
                  style: const TextStyle(color: Colors.white)))
            ],
          ),
        ],
      ),
    );
  }
}
